<?php

/* *********************************************************************************
File:					install.estateagent.php
EstateAgent Improved - a Joomla Component
Copyright:			2004 - 2006 by Darko Selesi

Year: 2007
Improved by: Manos Orfanoudakis - "vosk"
Homepage: 		http://www.eaimproved.eu
Version: 			1.3.x                     
License:			GNU/GPL http://www.gnu.org/copyleft/gpl.html
*************************************************************************** */


// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

function com_install() {
  global $database, $mosConfig_live_site, $mosConfig_absolute_path;
  //==========Remove old menu=========================
  $database->setQuery("DELETE FROM #__menu WHERE link LIKE '%option=com_estateagent%'");
  $database->query();
  
  $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/edit.png' WHERE admin_menu_link='option=com_estateagent&act=manageObj'");
  $result[0] = $database->query();  
  $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/categories.png' WHERE admin_menu_link='option=com_estateagent&act=manageCat'");
  $result[1] = $database->query();
  $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/config.png' WHERE admin_menu_link='option=com_estateagent&act=configEA'");
  $result[2] = $database->query();
  $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/credits.png' WHERE admin_menu_link='option=com_estateagent&act=EAinfo'");
  $result[3] = $database->query();
  $database->setQuery("SELECT count(*) FROM #__categories WHERE name = 'EstateAgents' AND section =  'com_contact_details' ");
  $count = $database->loadResult();
  if($count<1){
   	 $database->setQuery("INSERT INTO #__categories (parent_id, title, name, section, description,published ) VALUES ('0', 'EstateAgents', 'EstateAgents', 'com_contact_details', 'EA Contacts','1')");
  	$result[4] = $database->query();
  }
  else
  	$result[4] = true;
  // make pictures directory writable	
  @chmod($mosConfig_absolute_path."/components/com_estateagent/pictures", 0777);	
 
  // copy menu-images 
  $images = array("edit_f2.png","publish_f2.png","unpublish_f2.png","new_f2.png","delete_f2.png","mediamanager.png");
  
 
  foreach($images as $i){
  	$source = $mosConfig_absolute_path."/administrator/images/".$i;
  	$dest = $mosConfig_absolute_path."/images/".$i;
  	if(!file_exists($dest)) {
  		@copy($source,$dest);
  	}
  	
  }
  
  //=======================JOOMFISH TRANSLATION SUPPORT========================================
  if (file_exists($mosConfig_absolute_path . '/administrator/components/com_joomfish/config.joomfish.php'))
   {
   	//Introduced in 1.3.2
   	 $database->setQuery("UPDATE #__jf_content SET `reference_field` = 'locstate' WHERE `reference_field` = 'state' AND `reference_table` = 'estateagent'");
	 $database->query();
   
   @unlink( "$mosConfig_absolute_path/administrator/components/com_joomfish/contentelements/estateagent.xml");
   @unlink( "$mosConfig_absolute_path/administrator/components/com_joomfish/contentelements/estateagent_categories.xml");
   
  
   
	@rename( "$mosConfig_absolute_path/administrator/components/com_estateagent/jfce/estateagent.xml", 					
					"$mosConfig_absolute_path/administrator/components/com_joomfish/contentelements/estateagent.xml");

	@rename( "$mosConfig_absolute_path/administrator/components/com_estateagent/jfce/estateagent_categories.xml", 
					"$mosConfig_absolute_path/administrator/components/com_joomfish/contentelements/estateagent_categories.xml");
	
	

	@rmdir ("$mosConfig_absolute_path/administrator/components/com_estateagent/jfce");

	} 
	else 
	{ 
		@unlink( "$mosConfig_absolute_path/administrator/components/com_estateagent/jfce/estateagent.xml" ); 
		@unlink( "$mosConfig_absolute_path/administrator/components/com_estateagent/jfce/ce_name2.xml" ); 
		@rmdir ("$mosConfig_absolute_path/administrator/components/com_estateagent/jfce"); 
	} 
	$fields=$database->getTableFields( array('#__estateagent'));
	$fields=$fields['#__estateagent'];
	//===================PREVIOUS VERSIONS JF SEARCH TABLE REMOVAL================
	
	$database->setQuery("DROP TABLE  IF EXISTS #__estateagent_mirror");
	$database->query();
	
	//===================ALTER DATABASE=================================================
	//==== Migrate from < EA Imp 1.3.1a
	if(isset($fields['state'])){
	
		$database->setQuery("ALTER TABLE `#__estateagent` CHANGE `state` `locstate` TINYTEXT");
		$database->query();
	
		$database->setQuery("ALTER TABLE `#__estateagent` CHANGE `publish` `published` tinyint(1)");
		$database->query();
	
		$database->setQuery("ALTER TABLE `#__estateagent_categories` CHANGE `publish` `published` tinyint(1)");
		$database->query();
	
		$database->setQuery("ALTER TABLE `#__estateagent_images` CHANGE `publish` `published` tinyint(1)");
		$database->query();
	
	
	}
	//===1.3.2
	if(!isset($fields['mapcoords'])){
	
	$database->setQuery("ALTER TABLE `#__estateagent` ADD COLUMN `mapcoords` TINYTEXT NOT NULL DEFAULT '' AFTER `hot`");
	$database->query();
	
	}
	//===1.3.3
	if(!isset($fields['checked_out'])){
		$q = "ALTER TABLE `#__estateagent` ADD COLUMN `checked_out` tinyint(11) NOT NULL default '0', "
				."ADD COLUMN `checked_out_time` time default NULL";
		$database->setQuery($q);
		$database->query();
	}
	/*
	//=== Gallery images file type!:
	$q = "SELECT `title` , `fname` , `type` , `path` FROM #__estateagent_images WHERE objid = $oid AND (`type` = '.gif' OR type = '.png')";
	$database->setQuery($q);
	$res	= $database->loadObjectList();
	foreach($res as $img){
		@rename($mosConfig_absolute_path.$img->path.$img->fname.$img->type,$mosConfig_absolute_path.$img->path.$img->fname.".jpg");
		@rename($mosConfig_absolute_path.$img->path."t".$img->fname.$img->type,$mosConfig_absolute_path.$img->path."t".$img->fname.".jpg");

	}
	//Fix the table now...
	$q='UPDATE #__estateagent_images SET type = \'.jpg\' WHERE  (type = \'.gif\' OR type = \'.png\')';
	$database->setQuery($q);
	$database->query();
	*/
	
  ?>
  
<div style="text-align:left">
	<img src='<?php echo $mosConfig_live_site;?>/components/com_estateagent/pictures/gui/ealogo.png' alt='go to the home of the EstateAgent' border='0'><br /><br />
	<b>Congratulations!</b><br /><br />
  	***<span style="color:green; font-weight:bold">EstateAgent 1.3.x Improved installed successfully!</span>***
  <br /><br />
  Before you start to add your real estate objects,<br>please take a little time and...<br />
  <ol>
  	<li>Create categories for your real estate objects</li>
  	<li>Add your contact information to the Joomla <a href="<?php echo $mosConfig_live_site;?>/administrator/index2.php?option=com_contact" target="_blank">Contact &gt;&gt;&gt;Manage Contacts</a><br />
		(EstateAgent has created a special contact category called "EstateAgents". Please fill in your contact informations there.<br />
	EA Improved needs this information to show your company's address and contact information!)</li>
  	<li>Then go to the <b>EA configuration</b> and:</b><br /><br /></li>
  			<ul>
				<li>Select the right GD-library</li>
  				<li>Select the image and thumbnail width, heigth and quality</li>
  				<li>Check the other settings</li>
  				<li>If you wish to use the integrated <b>MAP24.com</b> support:<br />go to <a href="http://www.map24.com" target="_blank">MAP24.com</a> and create a chargefree <b>link2map</b> account</li>
				<li>If you wish to use the <b>GoogleMap :</b> <br />go to <a href="http://www.google.com/apis/maps/signup.html" target="_blank">GoogleMap</a> and get a valid key </li>
				
  			</ul><br />
	<li>If you like EstateAgent Improved and think, EA has saved you time and money:<br /><br />Please support us.<br />
				<br /><br /><b>Thank you!</b></li>
  </ol>
 </div>
<?php
}
?>